/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.ufc.quixada.servlet;

import br.com.ufc.quixada.comandos.CmdAlugar;
import br.com.ufc.quixada.comandos.CmdAtivarConta;
import br.com.ufc.quixada.comandos.CmdCadastrarCliente;
import br.com.ufc.quixada.comandos.CmdEnviarEmailAluguel;
import br.com.ufc.quixada.comandos.CmdEnviarEmailCadastro;
import br.com.ufc.quixada.comandos.CmdExibirCaracteristicasFilme;
import br.com.ufc.quixada.comandos.CmdLogin;
import br.com.ufc.quixada.comandos.CmdLogoff;
import br.com.ufc.quixada.comandos.CmdPedido;
import br.com.ufc.quixada.comandos.CmdVisualizarFilmes;
import br.com.ufc.quixada.comandos.CmdVisualizarFilmesAcao;
import br.com.ufc.quixada.comandos.CmdVisualizarFilmesAnimacao;
import br.com.ufc.quixada.comandos.CmdVisualizarFilmesBluRay;
import br.com.ufc.quixada.comandos.CmdVisualizarFilmesComedia;
import br.com.ufc.quixada.comandos.CmdVisualizarFilmesDVD;
import br.com.ufc.quixada.comandos.CmdVisualizarFilmesDrama;
import br.com.ufc.quixada.comandos.CmdVisualizarFilmesNacional;
import br.com.ufc.quixada.comandos.CmdVisualizarFilmesSuspense;
import br.com.ufc.quixada.comandos.CmdVisualizarFilmesTerror;
import br.com.ufc.quixada.interfaces.Comando;
import java.io.IOException;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Fuscao
 */
public class ServletCentral extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private Hashtable comandos;
    private boolean debug = true;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // HttpSession session = request.getSession(true);
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String cmd = request.getParameter("comando");
        //if (debug) {
        //  System.out.println("Cmd:" + cmd);
        //}
        Comando comando = (Comando) comandos.get(cmd);

        try {
            String tela = comando.executa(request, response);
            if (tela != null && !tela.trim().equals("")) {
                if (debug) {
                    System.out.println("Tela:" + tela);
                }
                response.sendRedirect(request.getContextPath() + tela);
            }
        } catch (Exception ex) {
            response.sendRedirect(request.getContextPath() + "/index.jsp");
            Logger.getLogger(ServletCentral.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    
    /**
     *
     */
    @Override
    public void init() {
        Comando cmdo;
        comandos = new Hashtable();
        cmdo = new CmdAlugar();
        comandos.put("CmdAlugar", cmdo);
        cmdo = new CmdAtivarConta();
        comandos.put("CmdAtivarConta", cmdo);
        cmdo = new CmdCadastrarCliente();
        comandos.put("CmdCadastrarCliente", cmdo);
        cmdo = new CmdEnviarEmailAluguel();
        comandos.put("CmdEviarEmailAluguel", cmdo);
        cmdo = new CmdEnviarEmailCadastro();
        comandos.put("CmdEviarEmailCadastro", cmdo);
        cmdo = new CmdExibirCaracteristicasFilme();
        comandos.put("CmdExibirCaracteristicasFilme", cmdo);
        cmdo = new CmdLogin();
        comandos.put("CmdLogin", cmdo);
        cmdo = new CmdLogoff();
        comandos.put("CmdLogoff", cmdo);
        cmdo = new CmdPedido();
        comandos.put("CmdPedido", cmdo);
        cmdo = new CmdVisualizarFilmes();
        comandos.put("CmdVisualizarFilmes", cmdo);
        cmdo = new CmdVisualizarFilmesAcao();
        comandos.put("CmdVisualizarFilmesAcao", cmdo);
        cmdo = new CmdVisualizarFilmesAnimacao();
        comandos.put("CmdVisualizarFilmesAnimacao", cmdo);
        cmdo = new CmdVisualizarFilmesComedia();
        comandos.put("CmdVisualizarFilmesComedia", cmdo);
        cmdo = new CmdVisualizarFilmesDrama();
        comandos.put("CmdVisualizarFilmesDrama", cmdo);
        cmdo = new CmdVisualizarFilmesNacional();
        comandos.put("CmdVisualizarFilmesNacional", cmdo);
        cmdo = new CmdVisualizarFilmesSuspense();
        comandos.put("CmdVisualizarFilmesSuspense", cmdo);
        cmdo = new CmdVisualizarFilmesTerror();
        comandos.put("CmdVisualizarFilmesTerror", cmdo);
        cmdo = new CmdVisualizarFilmesDVD();
        comandos.put("CmdVisualizarFilmesDVD", cmdo);
        cmdo = new CmdVisualizarFilmesBluRay();
        comandos.put("CmdVisualizarFilmesBluRay", cmdo);
        


    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
